<template>
	<div class="bg-white">
		<Form :label-width="80">
			<FormItem  label="角色编码">
				<Input :value="role.roleCode" placeholder="请输入角色的编码"></Input>
			</FormItem>
			<FormItem  label="角色名">
				<Input v-model="role.name" placeholder="请输入角色的名称"></Input>
			</FormItem>
			<FormItem  label="权限">
				<permission-tree :permissions="allPermissions"></permission-tree>
			</FormItem>
			<FormItem class="text-right">
				<Button @click="submit" type="primary">提交</Button>
			</FormItem>
		</Form>
	</div>
</template>

<script>
	export default {
		data: function() {
			return {
				role: {
					roleCode: "",
					name: "",
					permissions: "[]",
				},
				permissions: [],
				allPermissions: []
			};
		},
		props: {
			params: {
				type: Object,
				default: undefined,
			}
		},
		methods: {
			recursionPermission: function(permissions) {
				for (let index in permissions) {
					let currentPermissionItem = permissions[index];
					
					if (currentPermissionItem.selected === true) {
						this.permissions.push(currentPermissionItem.code);
					}
					
					if (currentPermissionItem.permissions instanceof Array) {
						this.recursionPermission(currentPermissionItem.permissions);
					}
					
				}
			},
			recursionPermissionSelected: function(permissions) {
				console.log(2)
				console.log(this.role)
				for (let index in permissions) {
					let currentPermissionItem = permissions[index];
					
					if (this.role.permissions.search(currentPermissionItem.code) > 0) {
						currentPermissionItem.selected = true;
					}
					
					if (currentPermissionItem.permissions instanceof Array) {
						this.recursionPermissionSelected(currentPermissionItem.permissions);
					}
					
				}
			},
			submit: function() {
				
				if (this.role.roleCode.length === 0) {
					this.message.error("请输入角色编码");
					return;
				}
				
				if (this.role.name.length === 0) {
					this.message.error("请输入角色名");
					return;
				}
				
				this.permissions.splice(0);
				
				this.recursionPermission(this.allPermissions);

				if (this.permissions.length === 0) {
					this.message.error("请选择权限");
					return;
				}

				this.role.permissions = JSON.stringify(this.permissions);
				console.log(this.role);
				this.http.put({
					url: "/role/info",
					data: this.role,
				}).then(data => {
					this.message.info("修改成功");
					this.$router.detail.close();
				});
			},
			init: function() {
				this.searchPermission();
			},
			searchPermission: function() {
				this.http.get({
					url: "/role/info",
					param: {
						roleCode : this.role.roleCode
					}
				}).then(data => {
					this.role = data.role;
					console.log(this.role);
				}).then(() => {
					this.http.get({
						url: "/permission/all",
					}).then(data => {
						this.allPermissions = data;
						this.recursionPermissionSelected(this.allPermissions);
					});
				});
			}
		},
		created: function () {
			this.role.roleCode = this.params.roleCode;
			console.log(this.role.roleCode);
			this.init();
		},
	}
</script>

<style>
</style>
